<?php

namespace app\user\controller;

use app\common\respond\HttpApi;
use app\user\logic\UserLogic;

class UserController
{
    /**
     * @Description 获取用户信息
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/31 15:04:11
     * @return null
     * @throws \think\db\exception\DbException
     */
    public function getUserInfo(HttpApi $respond)
    {
        $logic = new UserLogic();
        $data = $logic->getUserInfo();

        return $respond->success($data);
    }

    /**
     * @Description 修改密码
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/09 17:47:29
     * @return null
     */
    public function changePassword(HttpApi $respond)
    {
        $param = [
            'old_password' => request()->post('old_password'),
            'new_password' => request()->post('new_password'),
            'repassword' => request()->post('repassword'),
        ];
        $rule = [
            'old_password' => 'require|max:50',
            'new_password' => 'require|alphaDash|max:30',
            'repassword' => 'require|confirm:new_password',
        ];
        validate($rule)->check($param);

        $userId = request()->user_info['user_id'];
        $logic = new UserLogic();
        $logic->changePassword($userId, $param['old_password'], $param['new_password']);

        return $respond->success([]);
    }

    /**
     * @Description 获取用户详情
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/11 15:07:20
     * @return null
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'token' => request()->post("token"),
            'factory_id' => request()->post("factory_id")
        ];
        $rule = [
            'token' => 'require|>:0',
            'factory_id' => 'require|>:0'
        ];
        validate($rule)->check($param);

        $logic = new UserLogic();
        $data = $logic->detail($param['token'],$param['factory_id']);

        return $respond->success($data);
    }

    /**
     * @Description 修改用户信息
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/11 16:12:31
     * @return null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeUserInfo(HttpApi $respond)
    {
        $param = [
            'name' => request()->post('name'),
            'phone' => request()->post('phone'),
            'company' => request()->post('company'),
            'province' => request()->post('province'),
            'city' => request()->post('city'),
            'district' => request()->post('district'),
            'address' => request()->post('address'),
        ];
        $rule = [
            'name' => 'require|max:20',
            'phone' => 'require|mobile',
            'company' => 'require|max:50',
            'province' => 'require|max:15',
            'city' => 'require|max:15',
            'district' => 'require|max:15',
            'address' => 'require|max:50',
        ];
        validate($rule)->check($param);

        $logic = new UserLogic();
        $data = $logic->changeUserInfo($param);

        return $respond->success([]);
    }

    /**
     * @Description 找回密码
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/04 17:18:16
     * @return \think\response\Json
     */
    public function findPassword(HttpApi $respond)
    {
        $param = [
            'phone' => request()->post('phone'),
            'code' => request()->post('code'),
            'new_password' => request()->post('new_password'),
            'repassword' => request()->post('repassword'),
        ];
        $rule = [
            'phone' => 'require|mobile',
//            'code' => 'require',
            'new_password' => 'require|alphaDash|max:30',
            'repassword' => 'require|confirm:new_password',
        ];
        validate($rule)->check($param);

        $logic = new UserLogic();
        $logic->findPassword($param['phone'], $param['code'], $param['new_password']);

        return $respond->success([]);
    }
}